/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2025 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "stm32can.h"
#include "gpio.h"
#include "canopen.h"
#include "timer5.h"
#include "soft_timer.h"

uint8_t TxData[8] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
 
uint16_t INDEX_POSITION_ACTUAL_VALUIE =0X6064;

uint16_t INDEX_CURRENT_ACTUAL_VALUIE =0X6078;

uint16_t IINDEX_VELOCITY_ACTUAL_VALUE = 0X606C;

uint16_t INDEX_STATUS_WORD = 0X6041;
uint16_t INDEX_POSITION_DEMANND_VALUE =0X6062;
uint16_t INDEX_VELOCITY_DEMANND_VALUE = 0X606C;


/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */

/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/

/* USER CODE BEGIN PV */

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);

void updata_actual_and_demannd_position(void *arg)
{
      UNS8 dataType;
      UNS32 check_error = 0; // 使用 CanFestival 的 UNS32 类型
      // setODentry(&slave_ObjDictData, 
      //             INDEX_POSITION_ACTUAL_VALUIE, // 确保索引正确
      //             0,                           // 子索引
      //             &Position_actual_value,                   // 值指针
      //             sizeof(Position_actual_value),               // 值大小
      //             &check_error);
    //  setODentry(&slave_ObjDictData,
    //              INDEX_POSITION_DEMANND_VALUE, // 确保索引正确
    //               0,   &Position_demannd_value,                   // 值指针
    //               sizeof(Position_demannd_value),               // 值大小
    //               &check_error);
    Position_actual_value = HAL_GetTick();
    Position_demannd_value = Target_position;
    //  getODentry(&slave_ObjDictData,
    //              INDEX_POSITION_DEMANND_VALUE, // 确保索引正确
    //               0,   &Position_demannd_value,                   // 值指针
    //               sizeof(Position_demannd_value),               // 值大小
    //               &dataType,
    //               &check_error); 
 }
void updata_actual_and_demannd_velocity(void *arg)
{
      UNS32 check_error = 0; // 使用 CanFestival 的 UNS32 类型
      // writeLocalDict(&slave_ObjDictData, 
      //             IINDEX_VELOCITY_ACTUAL_VALUE, // 确保索引正确
      //             0,                           // 子索引
      //             &Velocity_actual_value,                   // 值指针
      //             sizeof(Velocity_actual_value),               // 值大小
      //             &check_error);     
      Velocity_actual_value = HAL_GetTick();
      Velocity_demand_value = Target_velocity;
      //  writeLocalDict(&slave_ObjDictData, 
      //             IINDEX_VELOCITY_ACTUAL_VALUE, // 确保索引正确
      //             0,                           // 子索引
      //             &Velocity_actual_value,                   // 值指针
      //             sizeof(Velocity_actual_value),               // 值大小
      //             &check_error);
      //Velocity_actual_value = HAL_GetTick();

}
/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */

/* USER CODE END 0 */

/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{


  /* USER CODE BEGIN 1 */

  /* USER CODE END 1 */

  /* MCU Configuration--------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_TIM5_Init();
  MX_CAN1_Init();

  if (HAL_CAN_Start(&hcan1) != HAL_OK)
  {
    Error_Handler();
  }
  if (HAL_CAN_ActivateNotification(&hcan1, CAN_IT_RX_FIFO0_MSG_PENDING) != HAL_OK)
  {
    Error_Handler();
  }


    unsigned char nodeID = 0x01;                   //主站ID
    setNodeId(&slave_ObjDictData,nodeID);
    setState(&slave_ObjDictData, Initialisation);		 //节点初始化
    setState(&slave_ObjDictData, Pre_operational);	
    // 正确：在循环外声明
 UNS32 check_error = 0; // 使用 CanFestival 的 UNS32 类型
 int i=0;
 UNS16 j=0;
 uint32_t timer_tick_;

 soft_timer_t *timer_10ms;
 soft_timer_t *timer_1s;
 soft_timer_t *timer_1ms;
 timer_10ms = soft_timer_create(10,SOFT_TIMER_PERIODIC,updata_actual_and_demannd_velocity,NULL);

 timer_1ms = soft_timer_create(1,SOFT_TIMER_PERIODIC,updata_actual_and_demannd_position,NULL);

 soft_timer_start(timer_1ms);
 soft_timer_start(timer_10ms);
  while(1){
    // timer_tick_=HAL_GetTick();
    // CAN_TxHeaderTypeDef TxHeader;
    // uint32_t TxMailbox;

    // // 配置 CAN 发送帧
    // TxHeader.StdId = 0x123;          // 标准 ID
    // TxHeader.ExtId = 0;              // 扩展 ID（不使用）
    // TxHeader.IDE = CAN_ID_STD;       // 标准帧
    // TxHeader.RTR = CAN_RTR_DATA;     // 数据帧
    // TxHeader.DLC = 8;                // 数据长度
    // TxHeader.TransmitGlobalTime = DISABLE;

    // // 发送 CAN 消息
    
    // if (HAL_CAN_AddTxMessage(&hcan1, &TxHeader, TxData, &TxMailbox) != HAL_OK)
    // {
    //   Error_Handler();
    // }
    // i++;
    // j++;
    soft_timer_process();
     HAL_Delay(1); // 每秒发送一次
    //   //setState(&slave_ObjDictData, Operational);	
    // Position_actual_value = HAL_GetTick();
    // Velocity_actual_value =i;
    // Statusword = j;
    // setODentry(&slave_ObjDictData, 
    //               INDEX_POSITION_ACTUAL_VALUIE, // 确保索引正确
    //               0,                           // 子索引
    //               &Position_actual_value,                   // 值指针
    //               sizeof(Position_actual_value),               // 值大小
    //               &check_error);               // 错误指针
    // setODentry(&slave_ObjDictData, 
    //               INDEX_STATUS_WORD, // 确保索引正确
    //               0,                           // 子索引
    //               &Statusword,                   // 值指针
    //               sizeof(Statusword),               // 值大小
    //               &check_error);
    // setODentry(&slave_ObjDictData, 
    //               IINDEX_VELOCITY_ACTUAL_VALUE, // 确保索引正确
    //               0,                           // 子索引
    //               &Velocity_actual_value,                   // 值指针
    //               sizeof(Velocity_actual_value),               // 值大小
    //               &check_error);
//readLocalDict()
   


  }
}

/**
  * @brief System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  /** Configure the main internal regulator output voltage
  */
  __HAL_RCC_PWR_CLK_ENABLE();
  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);

  /** Initializes the RCC Oscillators according to the specified parameters
  * in the RCC_OscInitTypeDef structure.
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  RCC_OscInitStruct.PLL.PLLM = 8;
  RCC_OscInitStruct.PLL.PLLN = 336;
  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  RCC_OscInitStruct.PLL.PLLQ = 4;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }

  /** Initializes the CPU, AHB and APB buses clocks
  */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
  {
    Error_Handler();
  }
}

/* USER CODE BEGIN 4 */

/* USER CODE END 4 */

/**
  * @brief  This function is executed in case of error occurrence.
  * @retval None
  */
void Error_Handler(void)
{
  /* USER CODE BEGIN Error_Handler_Debug */
  /* User can add his own implementation to report the HAL error return state */
  __disable_irq();
  while (1)
  {
  }
  /* USER CODE END Error_Handler_Debug */
}
#ifdef USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{
  /* USER CODE BEGIN 6 */
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
